Skip to content

Handling Moodle error messages (ENT-9080)#2563

Open
shravani-sonata-gottapu wants to merge 4 commits intoopenedx:masterfrom
shravani-sonata-gottapu:gshravani/ENT-9080
Open

Handling Moodle error messages (ENT-9080)#2563
shravani-sonata-gottapu wants to merge 4 commits intoopenedx:masterfrom
shravani-sonata-gottapu:gshravani/ENT-9080

Conversation

@shravani-sonata-gottapu
Copy link

@shravani-sonata-gottapu shravani-sonata-gottapu commented Mar 9, 2026

Description :
Moodle returns 200 response status code on failed transmission jobs which is the historical and default behavior of Moodle web services.

Changes :
Added 400 responses and mapped them.

Testing :
Local testing is not possible due to unavailability of data. we can test directly in stage/prod to verify the errors.

Copy link

@rgopalrao-sonata-png rgopalrao-sonata-png left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the comments and update

Comment on lines +93 to +100

# Define mapped_status based on error_code
mapped_status = {
'invalidtoken': 'Invalid Token',
'missingfield': 'Missing Field',
'duplicatedata': 'Duplicate Data',
}.get(error_code, 'Unknown Error')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is moodle response :HTTP/1.1 200 OK
Content-Type: application/json

{
"errorcode": "missingparam",
"message": "A required parameter is missing."
}
This will still return elif error_code:

raise MoodleClientError(
    'Moodle API Client Task "{method}" failed with error code '
    '"{code}" and message: "{msg}" '.format(...),
    response.status_code,  # ( This is will return 200 status code)!
    moodle_error={'error_code': error_code, ...}
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can have a function like to correct the status
def _effective_error_status_code(status_code, error_code=None):
"""Map Moodle error codes or convert false-200s to failure statuses"""
if error_code and error_code in MOODLE_ERROR_STATUS_MAP:
return MOODLE_ERROR_STATUS_MAP[error_code]
if status_code is None or status_code < 400:
return 555 # Semantic error
return status_code

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elif error_code:
status_code = _effective_error_status_code(response.status_code, error_code)
raise MoodleClientError(..., status_code, moodle_error={...}) u can call the function to change the status code

from integrated_channels.integrated_channel.client import IntegratedChannelApiClient
from integrated_channels.utils import generate_formatted_log, stringify_and_store_api_record

MOODLE_ERROR_STATUS_MAP = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please look into this https://2u-internal.atlassian.net/wiki/spaces/SOL/pages/edit-v2/1081409695?draftShareId=33d153f3-4beb-45a9-a403-aee6f781e5e1
MOODLE_ERROR_STATUS_MAP = {
# Duplicate/Conflict errors
"shortnametaken": 409,
"courseidnumbertaken": 409,
"morethanonerecordinfetch": 409,
# Not Found errors
"cannotfindcourse": 404,
"cannotfinduser": 404,
"invalidrecord": 404,
"usernamenotfound": 404,
# Bad Request errors
"missingparam": 400,
"codingerror": 400,
# Authentication/Authorization errors
"invalidlogin": 401,
"invalidtoken": 401,
"accessexception": 403,
"errorcatcontextnotvalid": 403,
"usernotfullysetup": 403,
# Server/Database errors
"dbconnectionfailed": 503,
"dmlreadexception": 503,
}Needs to be handled.

Copy link

@rgopalrao-sonata-png rgopalrao-sonata-png left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated code and resolved

@iloveagent57
Copy link
Contributor

@shravani-sonata-gottapu I don't have deep expertise on integrated channels stuff, but would it make more sense to include this change in https://github.com/openedx/enterprise-integrated-channels/blob/main/channel_integrations/moodle/client.py ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants